/*
 * File: DocumentSerializingTest.java 
 *  
 * Created on 01.02.2011
 */
package de.graph.common;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import de.graph.common.impl.GraphFactory;
import de.graph.common.impl.GraphFactory.GraphType;
import de.graph.document.DocumentManager;
import de.graph.document.impl.DocumentManagerImpl;

public class GraphSerializingTest {

    private Graph graph;

    private Vertex v1;

    @Before
    public void setUp() {
        this.graph = GraphFactory.create(GraphType.DIRECTED);

        assertNotNull(graph);

        // create graph
        this.v1 = graph.createVertex("v1");
        Vertex v2 = graph.createVertex("v2");
        Vertex v3 = graph.createVertex("v3");

        graph.createEdge(v1, v2);
        graph.createEdge(v2, v3);
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testSerializeGraph() throws IOException, ClassNotFoundException {

        Graph graph2 = (Graph) serializeAndDeserialize(graph);

        assertEquals(graph, graph2); // deep compare but documents only

        System.out.println("graph 1: " + graph);
        for (Vertex vertex : graph.getVertices()) {
            System.out.println(vertex);
            for (Edge edge : vertex.getOutEdges()) {
                System.out.println("  " + edge);
            }
        }

        System.out.println("graph 2: " + graph2);
        for (Vertex vertex : graph2.getVertices()) {
            System.out.println(vertex);
            for (Edge edge : vertex.getOutEdges()) {
                System.out.println("  " + edge);
            }
        }
    }

    @Test
    public void testSerializeVertex() throws IOException, ClassNotFoundException {

        Vertex v2 = (Vertex) serializeAndDeserialize(v1);

        assertEquals(v1, v2);
    }

    private Object serializeAndDeserialize(Object obj) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
        ObjectOutputStream ooStream = new ObjectOutputStream(baoStream);

        ooStream.writeObject(obj);
        ooStream.close();

        System.out.println("StreamSize: "+ baoStream.size());

        // clear document manager
        // TODO - wenn es funktionieren würde, sollten auch Documente neu aufgebaut werden
        DocumentManager documentManager = DocumentManagerImpl.getInstance();        
        documentManager.clear();

        ByteArrayInputStream baiStream = new ByteArrayInputStream(baoStream.toByteArray());
        ObjectInputStream oiStream = new ObjectInputStream(baiStream);

        Object obj2 = oiStream.readObject();
        oiStream.close();

        return obj2;
    }
}
